package etm.servlet.vivienda;


import java.io.IOException;
import java.sql.Date;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import etm.negocio.GestionQueja;
import etm.negocio.GestionVivienda;
import etm.excepcion.DAOExcepcion;
import upc.condominio.modelo.Queja;
import upc.condominio.modelo.Vivienda;

/**
 * Servlet implementation class QuejaBuscaServlet
 */
@WebServlet("/ViviendaBuscarServlet")
public class ViviendaBuscarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViviendaBuscarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		System.out.println("Dentro de doPost del servlet ViviendaBuscarServlet");
		System.out.println("accion==>"+request.getParameter("accion"));
		System.out.println("idViviendaSel==>"+request.getParameter("idViviendaSel"));
		GestionVivienda negocio = new GestionVivienda();
		Collection<Vivienda> lista = null;
		try {
			
			if (request.getParameter("accion")==null || request.getParameter("accion").equalsIgnoreCase("buscar")){			
				if (request.getParameter("txtApellidos") != null){
					String idUbicacion=request.getParameter("cmbUbicacion");
					String apellido=request.getParameter("txtApellidos");
					lista = negocio.buscarPorResidente(apellido,idUbicacion);
				}else{
					lista = negocio.listar();
				}	
				request.setAttribute("ViviendasLista", lista);
				RequestDispatcher rd = request.getRequestDispatcher("ViviendaLista.jsp");
				rd.forward(request, response);
			}else if (request.getParameter("accion")!=null && request.getParameter("accion").equalsIgnoreCase("editar")){
				//editar
				int idVivienda = Integer.parseInt(request.getParameter("idViviendaSel"));
				Vivienda vo = negocio.buscarPorID(idVivienda);
				System.out.println("edita numero " + vo.getNumero());
				request.setAttribute("vivienda", vo);
				RequestDispatcher rd = request.getRequestDispatcher("ViviendaEdit.jsp");
				rd.forward(request, response);
			}
			

		} catch (DAOExcepcion e) {
			System.out.println(e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
	}

}
